/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.block;

import org.statewalker.parser.wiki.grammar.WikiBlockDict;
import org.statewalker.tokenizer.AbstractTokenizer;
import org.statewalker.tokenizer.CharStream;
import org.statewalker.tokenizer.StreamToken;
import org.statewalker.tokenizer.CharStream.Marker;
import org.statewalker.tokenizer.CharStream.Pointer;
import org.statewalker.tokenizer.base.NewLineTokenizer;

public class ListTokenizer extends AbstractTokenizer {

    public ListTokenizer() {
        this(WikiBlockDict.LI);
    }

    public ListTokenizer(String key) {
        super(key);
    }

    protected boolean isListMarker(char ch) {
        return ch == '*' || ch == '#';
    }

    protected boolean isSpace(char ch) {
        return ch == ' ' || ch == '\t';
    }

    public StreamToken read(CharStream stream) {
        boolean result = false;
        Marker marker = stream.markPosition();
        try {
            Pointer pointer = marker.getPointer();
            boolean newLine = pointer.column == 0
                || NewLineTokenizer.readNewLine(stream) != null;
            if (!newLine)
                return null;

            char ch = stream.getChar();
            while (isSpace(ch)) {
                if (!stream.incPos())
                    return null;
                ch = stream.getChar();
            }
            while (isListMarker(ch)) {
                result = true;
                stream.incPos();
                ch = stream.getChar();
            }
            if (!result)
                return null;
            StreamToken token = newToken(stream, marker);
            return token;
        } finally {
            marker.close(!result);
        }
    }
}